import { FormSchema } from '/@/components/Table';
import dayjs from 'dayjs';

import { useI18n } from '/@/hooks/web/useI18n';
import { useRegisterbannerjumpTo } from '/@/views/common/commonSetting';
import eventBus from '/@/utils/eventBus';

const { t } = useI18n();

const { bannerjumpTo } = useRegisterbannerjumpTo();

const labelWidth = 160;

export function useTaskTypeOptions() {
  // const tylist = [];
  const tylist = history.state.list || [];
  const validate_type = history.state.validate_type || [];
  const filtered = tylist.filter((item) => item === 3);
  const { t } = useI18n();
  /** 任务类型列表 */
  const taskTypeOptions = [
    { label: t('sys.login.registerButton'), value: 1, disabled: tylist.includes(1) }, //
    { label: t('sys.login.download'), value: 2, disabled: tylist.includes(2) }, //
    { label: t('common.verify'), value: 3, disabled: filtered.length > 1 }, //
    {
      label: t('table.report.report_deposit'),
      value: 4,
      // disabled: tylist.filter((item) => item === 4) > 2,
    }, //
    {
      label: t('table.report.report_bet'),
      value: 5,
      // disabled: tylist.filter((item) => item === 5) > 2,
    }, //
    // { label: t('table.report.report_deposit'), value: 4, disabled: tylist.includes(4) }, //
    // { label: t('table.report.report_bet'), value: 5, disabled: tylist.includes(5) }, //
    // { label: t('table.discountActivity.discount_custom'), value: 6, disabled: tylist.includes(6) }, //
  ];

  /** 验证项目 */
  const taskVerifyItemsOptions = [
    { label: t('common.verify_email'), value: 1, disabled: validate_type.includes(1) },
    { label: t('common.verify_phone'), value: 2, disabled: validate_type.includes(2) },
  ];
  // console.log('111 taskV ', tylist, validate_type, filtered);
  // console.log('111 22 taskV ', taskTypeOptions);

  return { taskTypeOptions, taskVerifyItemsOptions };
}

/** 基本信息表单 */
export const schemas1 = () => {
  const { taskTypeOptions, taskVerifyItemsOptions } = useTaskTypeOptions();
  return [
    {
      field: 'id',
      component: 'Input',
      show: false,
    },
    {
      field: 'state',
      component: 'Input',
      show: false,
    },
    {
      field: 'cate_name',
      component: 'Input',
      show: false,
    },
    // {
    //   field: 'basic-info1',
    //   component: 'Input',
    //   label: '',
    //   // colProps: {
    //   //   span: 24,
    //   // },
    //   slot: 'basicInfo1',
    // },
    // {
    //   field: 'task_params',
    //   label: t('table.discountActivity.task_params'),
    //   component: 'Select',
    //   labelWidth,
    //   defaultValue: 1,
    //   colProps: {
    //     span: 12,
    //   },
    //   componentProps: {
    //     options: [
    //       {
    //         label: '不复制',
    //         value: 1,
    //       },
    //       {
    //         label: '任务1',
    //         value: 2,
    //       },
    //     ],
    //     getPopupContainer: () => document.body,
    //   },
    //   slot: 'task_params',
    // },
    // {
    //   field: 'basic-block',
    //   component: 'Input',
    //   label: '',
    //   colProps: {
    //     span: 12,
    //   },
    //   slot: 'basicBlock1',
    // },
    {
      field: 'lang',
      component: 'CheckboxGroup',
      label: t('v.discount.activity.type_lange'), //开放终端
      rules: [{ required: true }],
      labelWidth,
      // defaultValue: ['zh_CN'],
      slot: 'terminalSlot',
      helpMessage: '-',
      helpComponentProps: {
        text: `<span style='color:#FACD91;'>显示语言：</span>默认全选，勾选对应的语言活动会在对应的语言显示`,
        placement: 'right',
        isBefore: true,
      },
      colProps: {
        span: 24,
        class: 'mt-20px',
      },
    },
    {
      field: 'cate_id',
      label: t('table.discountActivity.task_category'),
      component: 'Select',
      labelWidth,
      defaultValue: '0',
      colProps: {
        span: 12,
      },
      componentProps: () => {
        return {
          onChange: (e, option) => {},
          options: [
            {
              label: t('table.discountActivity.task_category_0'),
              value: '0',
            },
          ],
        };
      },
    },
    {
      field: 'names',
      label: t('table.discountActivity.task_name'),
      labelWidth,
      component: 'Input',
      required: true,
      slot: 'zhNameTextSlot',
      rules: [
        {
          required: true,
          trigger: 'blur',
          validator(_rule, value) {
            if (typeof value === 'undefined') return Promise.reject('请输入活动名称');
            return Promise.resolve();
          },
        },
        {
          max: 30,
        },
      ],
      colProps: {
        span: 12,
      },
    },
    {
      field: 'ty',
      label: t('table.discountActivity.ty'),
      component: 'Select',
      labelWidth,
      helpMessage: '-',
      helpComponentProps: {
        text: `<span style='color:#FACD91;'>任务类型：</span><br/>
         注册：用于判断用户是否成功注册发放奖金。<br/>
         下载:用于判断用户是否成功下载发放奖金。(例如:下载APP)<br/>
         验证:用于判断用户是否成功验证发放奖金。(例如:邮箱验证)<br/>
         存款:用于判断用户是否成功存款发放奖金。(例如:累计完成存款条件)<br/>
         投注:用于判断用户是否成功投注发放奖金。(例如:累计完成投注条件)`,
        placement: 'right',
        isBefore: true,
      },
      defaultValue: null,
      required: true,
      colProps: {
        span: 12,
      },
      componentProps: () => {
        return {
          options: taskTypeOptions,
          getPopupContainer: () => document.body,
          onChange: (e) => {
            console.log('111', e);
            eventBus.emit('tyChange', e);
          },
        };
      },
    },
    {
      field: 'validate_type',
      label: t('table.discountActivity.task_verify_items'),
      component: 'Select',
      labelWidth,
      defaultValue: null,
      required: true,
      colProps: {
        span: 12,
      },
      componentProps: {
        options: taskVerifyItemsOptions,
        getPopupContainer: () => document.body,
      },
      ifShow: ({ values }) => {
        return values.ty == 3; //任务类型：验证
      },
    },
    // {
    //   field: 'job_duration_type', //任务时间类型：1.长期, 2.指定时间范围
    //   label: t('table.discountActivity.task_time'),
    //   component: 'Select',
    //   labelWidth,
    //   defaultValue: 1,
    //   required: true,
    //   colProps: {
    //     span: 12,
    //   },
    //   componentProps: {
    //     options: [
    //       {
    //         label: t('table.discountActivity.task_time_2'),
    //         value: 1,
    //       },
    //       {
    //         label: t('table.discountActivity.task_time_1'),
    //         value: 2,
    //       },
    //     ],
    //     getPopupContainer: () => document.body,
    //   },
    // },
    {
      field: 'time',
      label: t('table.discountActivity.task_time'),
      labelWidth,
      component: 'RangePicker',
      colProps: {
        span: 12,
      },
      defaultValue: [dayjs().startOf('day').toDate(), dayjs().endOf('day').toDate()],
      rules: [
        {
          required: true,
          validator(_rule, value) {
            if (typeof value === 'undefined') return Promise.reject('请选择正确的开始结束时间');
            if (!value[0] || !value[1]) return Promise.reject('请选择正确的开始结束时间');
            // if (!value['start_at'] || !value['end_at'])
            //   return Promise.reject('请选择正确的开始结束时间');
            // console.log('value111', value, Promise.resolve());
            return Promise.resolve();
          },
        },
      ],
      componentProps: {
        allowClear: false,
        format: 'YYYY-MM-DD',
        placeholder: [t('business.common_start_time'), t('business.common_end_time')],
        getPopupContainer: () => document.body,
        style: { width: '100%' },
      },
      // ifShow: ({ values }) => {
      //   return values.job_duration_type == 2; //默认类型
      // },
    },
    {
      field: 'multiple',
      component: 'InputNumber',
      labelWidth,
      colProps: {
        span: 12,
      },
      label: t('table.discountActivity.discount_audit_multiple'),
      required: true,
      componentProps: () => {
        return {
          stringMode: true,
          precision: 2,
          min: 0,
          onChange: (v) => {},
        };
      },
    },
    {
      field: 'detail',
      label: t('table.discountActivity.task_statement'),
      component: 'Input',
      labelWidth,
      // required: true,
      colProps: {
        span: 12,
      },
      slot: 'detailTextSlot',
    },
    {
      field: 'cycle',
      component: 'RadioGroup',
      label: t('table.discountActivity.discountActivity_cycle_mode'),
      required: true,
      defaultValue: 1,
      labelWidth: 120,
      colProps: {
        span: 24,
      },
      helpMessage: '-',
      helpComponentProps: {
        text: `<div><span style='color:#FFCB00;'>${t(
          'table.discountActivity.discountActivity_cycle_mode',
        )}: </span>${t('table.discountActivity.discountActivity_cycle_mode_tips')}</div>
                  `,
        placement: 'right',
        isBefore: true,
      },
      componentProps: ({ formModel }) => {
        return {
          options: [
            { label: t('table.discountActivity.discountActivity_cycle_mode_tips1'), value: 1 },
            { label: t('table.discountActivity.discountActivity_cycle_mode_tips2'), value: 2 },
            { label: t('table.discountActivity.discountActivity_cycle_mode_tips3'), value: 3 },
          ],
          onChange: (e) => {
            const val = e.target ? e.target.value : e;
            eventBus.emit('cycleCharge', val);
          },
        };
      },
      ifShow: ({ values }) => [4, 5].includes(values.ty),
      dynamicDisabled: ({ values }) => {
        return !!values.id;
      },
    },
  ];
};

/** 任务规则表单 */
export const schemas2: FormSchema[] = [
  {
    field: 'basic-info2',
    component: 'Input',
    label: '',
    colProps: {
      span: 24,
    },
    slot: 'basicInfo2',
  },
  {
    field: 'platform_range',
    label: t('v.discount.activity.Venue_statistics'),
    labelWidth: 120,
    component: 'RadioGroup',
    defaultValue: 1,
    colProps: {
      class: 'pop_componentProps',
      span: 9,
    },
    helpMessage: '-',
    helpComponentProps: {
      text: `<span style='color:#FACD91;'>统计场馆：</span>只统计选中的场馆的数据`,
      placement: 'right',
      isBefore: true,
    },
    componentProps: {
      options: [
        {
          label: t('common.all_venues'),
          value: 1,
        },
        {
          label: t('common.Designated_venue'),
          value: 2,
        },
      ],
      onChange: (value) => {
        const data = value?.target?.value || value || 'getself';
        eventBus.emit('change_Venue_statistics', data);
      },
    },
    ifShow: ({ values }) => {
      return [5].includes(values.ty);
    },
  },
  {
    field: 'platform_ids',
    component: 'InputNumber',
    slot: 'NegativeConfigType',
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) => {
      return [5].includes(values.ty);
    },
  },
  {
    field: 'configuration',
    label: t('v.discount.activity.configuration'),
    labelWidth: 120,
    component: 'RadioGroup',
    defaultValue: 1,
    colProps: {
      class: 'pop_componentProps',
      span: 9,
    },
    // helpMessage: '-',
    // helpComponentProps: {
    //   // text: `<span style='color:#FACD91;'>统计场馆：</span>只统计选中的场馆的数据`,
    //   placement: 'right',
    //   isBefore: true,
    // },
    componentProps: {
      options: [
        {
          label: t('v.discount.activity.uniform'),
          value: 1,
        },
        {
          label: t('v.discount.activity.Separate'),
          value: 2,
        },
      ],
      // getPopupContainer: () => document.body,
      onChange: (value) => {
        // console.log(value, '111data');
        const data = value?.target?.value || value || 'getself';
        eventBus.emit('change_configuration', data);
      },
    },
    ifShow: ({ values, model }) => {
      return [5].includes(values.ty) && model.platform_range == 2;
      // return [5].includes(values.ty);
    },
  },
  {
    field: 'CurrencyConfig',
    component: 'InputNumber',
    labelWidth,
    label: t('v.discount.activity.CurrencyConfig'),
    slot: 'CurrencyConfig',
    colProps: {
      span: 24,
    },
  },
  {
    field: 'rewardConfig',
    label: '',
    component: 'Input',
    labelWidth,
    slot: 'commonTable',
    colProps: {
      span: 24,
    },
    ifShow: ({ values }) => {
      return [4, 5].includes(values.ty);
    },
  },
  {
    field: 'award',
    component: 'InputNumber',
    labelWidth,
    colProps: {
      span: 12,
    },
    label: t('v.discount.activity.amount_bonus1'),
    required: true,
    componentProps: () => {
      return {
        stringMode: true,
        precision: 2,
        min: 0,
        onChange: (value) => {
          // console.log(value, '111data');
          const data = value?.target?.value || value || '';
          eventBus.emit('change_award', data);
        },
      };
    },
    ifShow: ({ values }) => {
      return ![4, 5].includes(values.ty);
    },
  },
  {
    field: 'button',
    label: t('business.banner_button_text'),
    labelWidth,
    colProps: {
      span: 12,
    },
    component: 'Input',
    required: true,
    slot: 'buttonTextSlot',
  },
  {
    field: 'button_type',
    label: t('business.banner_go_next'),
    component: 'Select',
    defaultValue: 1,
    required: true,
    labelWidth,
    colProps: {
      span: 12,
    },
    componentProps: {
      options: bannerjumpTo,
    },
  },
  {
    field: 'button_redirect',
    label: t('business.button_redirect_type'),
    required: true,
    labelWidth,
    colProps: {
      span: 12,
    },
    component: 'Input',
    ifShow: ({ values }) => {
      return values.button_type === 1;
    },
  },
];
/** 限制规则表单 */
export const schemas3: FormSchema[] = [
  {
    field: 'basic-info3',
    component: 'Input',
    label: '',
    colProps: {
      span: 24,
    },
    slot: 'basicInfo3',
    ifShow: ({ values }) => {
      return ![2, 3].includes(values.ty);
    },
  },
  {
    field: 'device',
    label: t('table.discountActivity.task_same_device'),
    component: 'Select',
    labelWidth: 150,
    defaultValue: true,
    required: true,
    colProps: {
      span: 12,
    },
    componentProps: {
      options: [
        { label: t('business.common_yes'), value: true },
        { label: t('business.common_no'), value: false },
      ],
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => {
      return [1].includes(values.ty);
    },
  },
  {
    field: 'vip',
    label: t('table.discountActivity.vip_'),
    component: 'Select',
    helpMessage: '-',
    helpComponentProps: {
      text: `<span style='color:#FACD91;'>VIP对象限制：</span>若等级被选中，则不具备参与条件，用户端不会展示该任务`,
      placement: 'right',
      isBefore: true,
    },
    labelWidth,
    defaultValue: 1,
    required: true,
    colProps: {
      span: 24,
    },
    slot: 'vip',
    componentProps: {
      options: [
        { label: t('business.common_yes'), value: 1 },
        { label: t('business.common_no'), value: 2 },
      ],
      getPopupContainer: () => document.body,
    },
    ifShow: ({ values }) => {
      return [4, 5].includes(values.ty);
    },
  },
];
/** 导出所有表单 */
export function ProcessLeftData() {
  return [...schemas1(), ...schemas2, ...schemas3];
}
